#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mkp make_pair
#define mems(a, x) memset((a), (x), sizeof(a))

using namespace std;
typedef unsigned uint;
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef long double ldb;
typedef pair<ll, ll> pii;

#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 23, stdin), p1 == p2) ? EOF : *p1++)
char buf[1 << 23], *p1 = buf, *p2 = buf;
inline int read() {
    char c = getchar();
    int x = 0;
    for (; !isdigit(c); c = getchar()) ;
    for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    return x;
}

const int maxn = 10000100;
const ll mod = 1000000007;

int n, m, a[maxn], pw[maxn], q[maxn], hd = 1, tl;
ll f[maxn];

struct DS {
	ll l = 1, r = 0, mid = 1, pre[maxn], suf[maxn], a[maxn];
	
	inline void build() {
		if (l > r) {
			return;
		}
		mid = (l + r) >> 1;
		suf[mid + 1] = pre[mid] = 1e18;
		for (int i = mid; i >= l; --i) {
			suf[i] = min(suf[i + 1], a[i]);
		}
		for (int i = mid + 1; i <= r; ++i) {
			pre[i] = min(pre[i - 1], a[i]);
		}
	}
	
	inline void dell() {
		++l;
		if (l > mid) {
			build();
		}
	}
	
	inline void delr() {
		--r;
		if (r < mid) {
			build();
		}
	}
	
	inline void addr(ll x) {
		a[++r] = x;
		if (l == r) {
			build();
		} else {
			pre[r] = min(pre[r - 1], x);
		}
	}
	
	inline void updl(ll x) {
		a[l] = x;
		suf[l] = min(suf[l + 1], x);
	}
	
	inline ll query() {
		return min(suf[l], pre[r]);
	}
} Q;

void solve() {
	n = read();
	m = read();
	for (int i = 1; i <= n; ++i) {
		a[i] = read();
	}
	pw[0] = 1;
	for (int i = 1; i <= n; ++i) {
		pw[i] = 1LL * pw[i - 1] * 23 % mod;
	}
	ll ans = 0;
	for (int i = 1; i <= n; ++i) {
		while (hd <= tl && q[hd] <= i - m) {
			++hd;
			Q.dell();
		}
		while (hd <= tl && a[q[tl]] <= a[i]) {
			--tl;
			Q.delr();
		}
		q[++tl] = i;
		Q.addr(f[q[tl - 1]] + a[i]);
		Q.updl(f[max(0, i - m)] + a[q[hd]]);
		f[i] = Q.query();
		ans = (ans + f[i] % mod * pw[n - i] % mod) % mod;
	}
	printf("%lld\n", ans);
}

int main() {
	freopen("knight.in","r",stdin);
	freopen("knight.out","w",stdout);
	int T = 1;
	// scanf("%d", &T);
	while (T--) {
		solve();
	}
	return 0;
}

